package com.mall.controller;

import com.mall.service.BrandService;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.PmsBrand;
import com.mall.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,
                             String keyword) {
        PageResult pr = brandService.list(pageNum, pageSize, keyword);
        return CommonResult.success(pr);
    }


    /**
     * 获取全部品牌列表
     *
     * @param
     * @return
     */
    @GetMapping("/listAll")
    public CommonResult listAll() {
        List<PmsBrand> list = brandService.listAll();
        return CommonResult.success(list);
    }

    /**
     * 添加品牌
     *
     * @param brand
     * @return
     */
    @PostMapping("/create")
    public CommonResult save(@RequestBody PmsBrand brand) {
        brandService.save(brand);
        return CommonResult.success(null, "添加成功");
    }

    /**
     * 删除品牌
     *
     * @param
     * @return
     */
    @GetMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id) {
        brandService.delete(id);
        return CommonResult.success(null, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public CommonResult delete(@RequestParam List<Integer> ids) {
        brandService.deleteAll(ids);
        return CommonResult.success(null, "删除成功");
    }

    /**
     * 更新品牌
     *
     * @param id
     * @param brand
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Integer id, @RequestBody PmsBrand brand) {
        brandService.update(id, brand);
        return CommonResult.success(null, "修改成功");
    }

    /**
     * 根据编号查询品牌信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult queryBrandById(@PathVariable Integer id) {
        PmsBrand brand = brandService.queryBrandById(id);
        return CommonResult.success(brand);
    }


    /**
     * 批量更新厂家制造商状态
     *
     * @param factoryStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public CommonResult updateAllFactoryStatus( Integer factoryStatus,
                                                @RequestParam List<Integer> ids) {
        brandService.updateAllFactoryStatus(factoryStatus, ids);
        return CommonResult.success(null, "批量更新成功");
    }


    /**
     * 批量更新显示状态
     *
     * @param showStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/showStatus")
    public CommonResult updateAllShowStatus( Integer showStatus,
                                            @RequestParam List<Integer> ids) {
        brandService.updateAllShowStatus(showStatus, ids);
        return CommonResult.success(null, "批量更新成功");
    }
}
